//
//  StartView.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/21.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "StartView.h"

@implementation StartView

//重写init方法，当startView用alloc方法创建的时候机会调用进来，只调用一次
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
    }
    return self;
}

//当从nib中获取的时候，就调用这个方法，只调用一次
- (void)awakeFromNib
{
    //创建星星view
    [self _creatStartView];
}



#pragma mark -_creatStartView
//创建startView的子视图
- (void)_creatStartView
{
    //添加评分的星星图标
    UIImage *image1 = [UIImage imageNamed:@"yellow"];
    UIImage *image2 = [UIImage imageNamed:@"gray"];
    
    
    _yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, image1.size.width*5, image1.size.height)];
    _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, image2.size.width*5, image2.size.height)];
    
    
    _yellowView.backgroundColor = [UIColor colorWithPatternImage:image1];
    _grayView.backgroundColor = [UIColor colorWithPatternImage:image2];
    
    //将星星视图添加上去
    [self addSubview:_grayView];
    [self addSubview:_yellowView];
    
    //调整startView的宽度和高度
    
        //放大倍数
    float scale = self.frame.size.height/_grayView.frame.size.height;
    
    //调整了星星的尺寸，因为视图放大是根据中心点来放大的，位置需要调整回来
    _grayView.transform = CGAffineTransformMakeScale(scale, scale);
     _yellowView.transform = CGAffineTransformMakeScale(scale, scale);
    
    //调整星星的位置
    CGRect frame = _grayView.frame;
    frame.origin = CGPointZero;
    _grayView.frame = frame;
    
    
    _yellowView.frame = frame;
    
    
    
    
    //设置黄色星星的宽度，根据评分来获得
//    float width1 = _average/10.0 * _yellowView.frame.size.width;
    
    
}

#pragma mark -setAverage:重写set方法，将数据模型的数值传递到这里来,不然就没法获得modal里面的average数据
- (void)setAverage:(float)average
{
    _average = average;
    
    //调整StartView的位置及尺寸，叫系统调用layoutSubviews方法，获取了数据之后就刷新数据显示
    [self setNeedsLayout];
}

//不能自调用，是由系统自己调用
- (void)layoutSubviews
{
    
    [super layoutSubviews];
    
    //设置星星的宽度
    float width = _average/10.0*_grayView.frame.size.width;
    CGRect frame = _grayView.frame;
    frame.size.width = width;
    
    _yellowView.frame = frame;
    
}

@end
